package com.kitty.game.pet.model;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.PetStore;
import com.kitty.game.map.service.MapService;
import com.kitty.game.role.model.Role;
import lombok.Getter;
import lombok.Setter;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import java.io.Serializable;
import java.util.*;

@Setter
@Getter
@JsonIgnoreProperties(ignoreUnknown =true)
public class PetBox implements Serializable {

    /**
     * 当前参战宠物
     */
    private int fightPetId;
    /**
     * 当前掠阵宠物
     */
    private int supplyPetId;



    private int fightGhostPetId;

    /**
     * 当前掠阵地府宠物
     */
    private int supplyGhostPetId;

    private int fightYangPetId;

    /**
     * 当前掠阵阳间宠物
     */
    private int supplyYangPetId;


    private PetStore petStore = new PetStore();

    /**
     * 宠物天书 天书id --> 书
     */
    private Map<Integer, PetBook> books = new HashMap<>();

    /**
     * 所有宠物
     */
    private Map<Integer, Pet> allPets = new HashMap<>();
    /**
     * 是否购买超阶坐骑
     */
    private ArrayList<Integer> zuoqi = new ArrayList();

    /**
     * 是否购买超阶坐骑
     */
    private ArrayList<Integer> lipo = new ArrayList();
    /**
     * 是否购买超阶坐骑
     */
    private ArrayList<Integer> fashu = new ArrayList();

    public void addNewPet(Pet pet) {
        this.allPets.put(pet.getId(), pet);
    }

    public void removePet(Pet pet) {
        this.allPets.remove(pet.getId());
    }

    public void addBook(PetBook book) {
        this.books.put(book.getId(), book);
    }
    /**
     * 宠物栏
     * @return
     */
    public Set<Integer> equippedPets(Role role) {
        MapService mapService = SpringUtils.getBean(MapService.class);
        int type = mapService.roleInMapByType(role);
        if(type == 2){
            return yingjian();
        }else{
            return yangjian();
        }
//        Set<Integer> set = new HashSet<>();
//        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
//            Pet pet = entry.getValue();
//            if (pet.isInStore()) {
//                continue;
//            }
//            set.add(pet.getId());
//        }


    }
    public Set<Integer> equippedPets(String petName) {
        int yinJian = isYinJian(petName);
        if(yinJian == 1){
            return yingjian();
        }else{
            return yangjian();
        }
    }

    public Set<Integer> allPets() {
        Set<Integer> set = new HashSet<>();
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            Pet pet = entry.getValue();
            if (pet.isInStore()) {
                continue;
            }
            set.add(pet.getId());
        }

        return set;
    }

    public Set<Integer> yangjian(){
        Set<Integer> set = new HashSet<>();
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            Pet pet = entry.getValue();
            if (pet.isInStore()) {
                continue;
            }
            if (pet.getType() == 6 || pet.getType() == 7 || pet.getType() == 8) {
                continue;
            }
            set.add(pet.getId());
        }

        return set;
    }
    public int isYinJian(String petName){
        List<String> list = Arrays.asList("炼狱魔", "阴阳师", "魅灵", "狱獒","范无救", "牛头狱卒", "谢必安", "马面罗刹","冥炎之灵", "孟姑", "谛听", "阎摩");
        if(list.contains(petName)){
            return 1;
        }
        return 0;
    }

    public Set<Integer> yingjian(){
        Set<Integer> set = new HashSet<>();
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            Pet pet = entry.getValue();
            if (pet.isInStore()) {
                continue;
            }
            if (pet.getType() == 6 || pet.getType() == 7 || pet.getType() == 8){
                set.add(pet.getId());
            }
        }
        return set;
    }

    /**
     * 通过id查找宠物栏的宠物
     * @param petId
     * @return
     */
    public Pet getPetByPetId(int petId) {
        if (!allPets().contains(petId)) {
            return null;
        }
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            Pet pet = entry.getValue();
            if (pet.getId() == petId) {
                return pet;
            }
        }
        return null;
    }

    /**
     * 通过位置查找宠物栏的宠物
     * @param pos
     * @return
     */
    public Pet getEquippedPetByPosition(byte pos) {
        Set<Integer> equipped = allPets();
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            Pet pet = entry.getValue();
            if (!equipped.contains(pet.getId())) {
                continue;
            }
            if (pet.getPosition() == pos) {
                return pet;
            }
        }
        return null;
    }
    /**
     * 通过名称查找宠物栏的宠物
     * @param name
     * @return
     */
    public List<Pet> getEquippedPetByPetName(String name,Role role) {
        List<Pet> pets = new ArrayList<>();
        Set<Integer> equipped = allPets();
        List<Integer> fightIds = new ArrayList<>();
        fightIds.add(role.getPetBox().getFightGhostPetId());
        fightIds.add(role.getPetBox().getFightYangPetId());
        fightIds.add(role.getPetBox().getSupplyGhostPetId());
        fightIds.add(role.getPetBox().getSupplyYangPetId());
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            Pet pet = entry.getValue();
            if (!equipped.contains(pet.getId())) {
                continue;
            }
            if (name.equals(pet.getName())  && !fightIds.contains(pet.getId()) && pet.getLevel() == 1) {
                pets.add(pet);
            }
        }
        return pets;
    }

    /**
     * 通过位置查找所有的宠物
     * 加载宠物获取位置用
     * @param pos
     * @return
     */
    public Pet getEquippedPetByPosition(byte pos,boolean flag) {
        for (Map.Entry<Integer, Pet> entry : allPets.entrySet()) {
            Pet pet = entry.getValue();
            if (pet.getPosition() == pos) {
                return pet;
            }
        }
        return null;
    }
    /**
     * 通过位置查找宠物栏的宠物
     * @param pos
     * @return
     */
    public Pet getStorePetByPosition(short pos) {
        Map<Short,Integer> info = petStore.getPetInfo();
        Integer petId = info.get(pos);
        if (petId == null) {
            return null;
        }
        return allPets.get(petId);
    }


    public Map<Integer, PetBook> getPetBooks(int petId) {
        Map<Integer, PetBook> result = new HashMap<>();

        for (Map.Entry<Integer, PetBook> entry : books.entrySet()) {
            PetBook book = entry.getValue();
            if (book.getPetId() == petId) {
                result.put(entry.getKey(), book);
            }
        }
        return result;
    }
}
